﻿using System;
using System.Collections.Generic;
using System.Web;
using PayRoller.Code;
using System.Web.SessionState;
using System.Web.Profile;
using Huayou.UI.WebControls;
using System.Data;
using HySoft.Cash.Business;
using System.Web.UI.WebControls;
using Huayou.Common;
using HySoft.Cash.Structure;
using System.Globalization;
using PayRoller.UControls;

namespace PayRoller.Member
{
    public class InvoicesList : MemberParent, IRequiresSessionState
    {
        // Fields
        protected Huayou.UI.WebControls.DataGridTable dgdList;
        protected Label lblTip;
        protected TextBox txtParamstr;
        protected TXTFormatDate txtInvoiceDateBegin;
        protected TXTFormatDate txtInvoiceDateTo;
        protected TextBox txtInvoiceID;

        // Methods
        protected void dgdList_DataBinding(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            TinvoicelistBN tbn = new TinvoicelistBN(this.Page);
            tbn.QueryOrgSid(Convert.ToInt32(base.OrgSid));
            tbn.QueryIsValidNot(InvoiceStatuses.Deleted);
            
            bool successful = false;
            string dateBeginString = txtInvoiceDateBegin.Text.Trim();
            string dateEndString = txtInvoiceDateTo.Text.Trim();
            string invoceIDString = txtInvoiceID.Text.Trim();
            //DateTime dateBegin;
            //DateTime dateEnd;
            int invoiceID = 0;

            if (dateBeginString != string.Empty)
            {
                //successful = DateTime.TryParse(dateBeginString,AppSettings.CurrentCulture,DateTimeStyles.None,out dateBegin);
                //if (successful == true)
                //{
                //    tbn.QueryInvoiceDateBegin(dateBegin);
                //}

                tbn.QueryInvoiceDateBegin(txtInvoiceDateBegin.Value);
            }

            if (dateEndString != string.Empty)
            {
                //successful = DateTime.TryParse(dateEndString, AppSettings.CurrentCulture, DateTimeStyles.None, out dateEnd);
                //if (successful == true)
                //{
                //    tbn.QueryInvoiceDateEnd(dateEnd);
                //}

                tbn.QueryInvoiceDateEnd(txtInvoiceDateTo.Value);
            }

            if (invoceIDString != string.Empty)
            {
                successful = int.TryParse(invoceIDString,out invoiceID);
                if (successful==true && invoiceID > 0)
                {
                    tbn.QuerySid(invoiceID);
                }
            }

            tbn.Order = "Sid desc";
            dt = tbn.GetList();
            tbn.Close();
            tbn.Dispose();
            CommonBasePage.SetPage(this.dgdList, dt);
            base.AddValue("pageno", Convert.ToString((int)(this.dgdList.CurrentPageIndex + 1)));
            this.txtParamstr.Text = base.PackPart(base.ParamValue);
        }

        protected void dgdList_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
        }

        protected void dgdList_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.dgdList.CurrentPageIndex = e.NewPageIndex;
            this.dgdList.DataBind();
        }

        protected override void OnInit(EventArgs e)
        {
            base.CheckLogin();
            base.OnInit(e);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                base.ParamValue = base.UnPack();
                this.txtParamstr.Text = base.PackPart(base.ParamValue);
                string str = base.GetValue("pageno");
                if (str != null)
                {
                    this.dgdList.CurrentPageIndex = Convert.ToInt32(str) - 1;
                }
                this.dgdList.DataBind();
            }
            else
            {
                base.ParamValue = base.UnPackPart(this.txtParamstr.Text);
            }
        }

        // Properties
        protected HttpApplication ApplicationInstance
        {
            get
            {
                return this.Context.ApplicationInstance;
            }
        }

        protected DefaultProfile Profile
        {
            get
            {
                return (DefaultProfile)this.Context.Profile;
            }
        }

        protected void bnSearch_Click(object sender, EventArgs e)
        {
            this.dgdList.DataBind();
        }
    }

}
